package utils;

/**
 * Created by lining on 2018/3/1.
 */
public class HideUtils {

    public static final String startFlag = "************************************************************************************************************************";
    public static final String TYPE_ID_NUM = "idNum";
    public static final String TYPE_PHONE = "phone";
    public static final String TYPE_NAME = "name";
    public static final String TYPE_ADDRESS = "address";
    public static final String TYPE_BANK_NUM = "banknum";

    public static String desensitize(String type,String source){
        if(TYPE_ID_NUM.equalsIgnoreCase(type))return hide(source,3,4);//证件号码：前3后4
        else if (TYPE_PHONE.equalsIgnoreCase(type)) return hide(source,2,3);//手机号：前2后3
        else if (TYPE_NAME.equalsIgnoreCase(type)) return hide(source,1,1);//姓名：前1后1
        else if (TYPE_ADDRESS.equalsIgnoreCase(type)) return hide(source,2,2);//地址：前2后2
        else if (TYPE_BANK_NUM.equalsIgnoreCase(type)) return hide(source,6,4);//地址：前6后4
        else return source;
    }

    /**
     * @return
     */
    public static String hide(String source,int prefixLen,int suffixLen){
        int totalLen = prefixLen + suffixLen;
        source = source == null ? "" : source;
        if (source.length() > totalLen) {
            String prefix = source.substring(0, prefixLen);
            String suffix = source.substring(source.length() - suffixLen);
            String middle = startFlag.substring(0, source.length() - totalLen);
            return prefix + middle + suffix;
        } else if (source.length() <= totalLen && source.length() > prefixLen) {
            String prefix = source.substring(0, prefixLen);
            String middle = startFlag.substring(0, source.length() - prefixLen);
            return prefix + middle;
        } else {
            return source;
        }
    }

    // 身份证前3后4
    public static String idNumHide(String sourceStr) {
        return sourceStr != null && sourceStr.length() > 7
                ? sourceStr.substring(0, 3)
                + startFlag.substring(0, sourceStr.length() - 7)
                + sourceStr.substring(sourceStr.length() - 4)
                : sourceStr;
    }

    // 手机号前2后3
    public static String phoneHide(String sourceStr) {
        return sourceStr != null && sourceStr.length() > 5
                ? sourceStr.substring(0, 2)
                + startFlag.substring(0, sourceStr.length() - 5)
                + sourceStr.substring(sourceStr.length() - 3)
                : sourceStr;
    }

    // 姓名前1
    public static String nameHide(String sourceStr) {
        if(sourceStr != null && sourceStr.length() == 2)return sourceStr.substring(0, 1) + "*";
        return sourceStr != null && sourceStr.length() > 2
                ? sourceStr.substring(0, 1)
                + startFlag.substring(0, sourceStr.length() - 2)
                + sourceStr.substring(sourceStr.length() - 1)
                : sourceStr;
    }

}
